<template>
  <div>
    <div class="data">
      <div class="searchForm">
        <el-form :inline="true" :model="params" class="demo-form-inline">
          <div style="display: flex" >
            <el-form-item style="">
              <el-button type="primary" icon="el-icon-plus" @click="add"
                >增加</el-button
              >
              <el-button type="danger" icon="el-icon-delete" @click="removeMore"
                >批量删除</el-button
              >
            </el-form-item>
          </div>
        </el-form>
      </div>

    <div class="message">
      <div class="message1">
          <el-table
            :data="rows"
            stripe
            class="table"
            @selection-change="handleSelectionChange"
            >
            <el-table-column type="selection" width="45"> </el-table-column>
            <el-table-column prop="skuId" label="SKU编码" width="140">
            </el-table-column>
            <el-table-column prop="salePrice" label="售价" width="115">
            </el-table-column>
            <el-table-column prop="originalPrice" label="原价" width="115">
            </el-table-column>
            <el-table-column prop="productName" label="商品名称" width="135">
            </el-table-column>
            <el-table-column prop="imgs" label="轮播图图片" width="145">
                <template slot-scope="scope">
                <img
                    v-if="scope.row.imgs"
                    :src="scope.row.imgs"
                    style="width: 100px; height: 100px"
                />
                </template>
            </el-table-column>
            <el-table-column prop="props" label="属性" width="105">
            </el-table-column>
            <el-table-column prop="userId" label="直接供应商" width="105">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                <el-button
                    type="primary"
                    size="mini"
                    icon="el-icon-edit"
                    style="margin-right: 16px"
                    @click="edit(scope.row)"
                >
                    编辑
                </el-button>
                <el-popconfirm
                    title="确定删除此数据吗？"
                    @confirm="remove(scope.row)"
                >
                    <el-button
                    slot="reference"
                    size="mini"
                    icon="el-icon-delete"
                    type="danger"
                    >删除</el-button
                    >
                </el-popconfirm>
                </template>
            </el-table-column>
            </el-table>
          </div>

      </div>

      <el-pagination
        @size-change="sizeChange"
        @current-change="getList"
        :page-sizes="[5, 10, 20, 100, 200, 300, 400]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <div style="margin-top: 25px"></div>
    </div>
    <SpEdit ref="spEditer" @load="getList"></SpEdit>
  </div>
</template>

<script>
import SpEdit from "../spgl/danpinEdit.vue";
export default {
  name: "spglList",
  components: {
    SpEdit,
  },
  data() {
    return {
      ///查询参数
      params: {
        pageNum: 1,
        pageSize: 5,
      },
      rows: [],
      total: 0,
      selecteds: [],
    };
  },
  methods: {
    //打开新增弹窗
    add() {
      this.$refs.spEditer.add();
    },
    //打开编辑弹窗
    edit(row) {
      this.$refs.spEditer.edit(row);
    },
    ///获取列表
    getList(pageNum) {
      if (pageNum) this.params.pageNum = pageNum;
      this.$http({
        url: "/admin/danpin",
        params: this.params,
      }).then((res) => {
        console.log(res);
        this.total = res.total;
        this.rows = res.rows;
      });
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    //修改每页数据大小
    sizeChange(pageSize) {
      this.params.pageSize = pageSize;
      //this.params.pageNum = 1
      this.getList(1);
    },
    //修改当前页页号
    // currentChange(pageNum) {
    //   //this.params.pageNum = pageNum
    //   this.getList(pageNum)
    // },

    //删除
    remove(row) {
      console.log(row);
      this.$http({
        url: "/admin/danpin",
        method: "DELETE",
        params: { indexId: row._id },
      }).then((res) => {
        if (!res.errorMsg) {
          this.getList(1);
        }
      });
    },

    ///删除多个
    removeMore() {
      ///获取被选中的数据
      let checkedRowsPrmose = [];
      let success = [],
        fail = [];
      for (let i = 0; i < this.selecteds.length; i++) {
        checkedRowsPrmose.push(
          new Promise((resovle) => {
            let row = this.selecteds[i];
            this.$http({
              url: "/admin/danpin",
              method: "DELETE",
              params: { indexId: row._id },
            }).then((res) => {
              if (!res.errorMsg) {
                success.push(row._id);
              } else {
                fail.push(row._id);
              }
              resovle();
            });
          })
        );
      }
      console.log(checkedRowsPrmose);

      this.$confirm("此操作将删除选中的数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //删除
        Promise.all(checkedRowsPrmose).then(() => {
          this.$message({
            message: `批量删除完成，成功删除${success.length}条数据,删除失败${fail.length}`,
            duration: 3000,
          });
          this.getList(1);
        });
      });
    },

    ///多选
    handleSelectionChange(rows) {
      console.log(rows);
      ///存储选中数据
      this.selecteds = rows;
    },

    ///启用与停用
    statusChange(row) {
      console.log(row);
      this.$http({
        url: "/admin/danpin",
        method: "PUT",
        data: {
          indexId: row._id,
          status: row.status,
        },
      }).then((res) => {
        if (res.errorMsg) {
          this.getList();
        }
      });
    },
  },
  created() {
    this.getList();
  },
  mounted() {},
};
</script>
<style scoped>
.data {
  width: 100%;
  height: 50px;
  background-color: #ffffff;
  margin-top: 30px;
}
.searchForm {
  margin-top: 5px;
  margin-left: 10px;
}
.message {
  margin-top: 10px;
  width: 100%;
}
.pagination {
  margin-top: 5px;
}
</style>
